//Item集合
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:village_android/config/contants.dart';
import 'package:village_android/utils/common_utils.dart';
import 'package:village_android/utils/image_utils.dart';

String uploadAsset = "http://s.mmbb123.com/upload_asset.png";

class ImagesCollectionPage extends StatefulWidget {

  List items = [];
  ValueSingleCallback itemsCallBack;
  ImagesCollectionPage({this.itemsCallBack,this.items});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new ImagesCollection();
  }
}

class ImagesCollection extends State<ImagesCollectionPage> {

 
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if(widget.items.length == 0) {
      widget.items.add(uploadAsset);
    }
    int cols = (widget.items.length / 3 + widget.items.length % 3).round();

    return widget.items.length == 0 ? initUploadImage(): new Container(
        padding: EdgeInsets.all(3.0),
        height: ScreenUtil().setHeight(150 + 150 * (cols)),
        child: GridView.count(
          physics: new NeverScrollableScrollPhysics(),
          crossAxisCount: 3,
          padding: EdgeInsets.all(3.0),
          children: widget.items.map((item) {
            return _girdInkWell(context, item, () {
              if (item == uploadAsset) {
                ImageUtils.pickImageUpLoad(context,((result, b) {
                  setState(() {
                    widget.items.insertAll(1,result);
                  });
                  if(widget.itemsCallBack!=null){
                    widget.itemsCallBack(widget.items);
                  }
                }), ((error, b) {
                  CommonUtils.showToast(context, "上传失败");
                }));
              } else {
                onItemClick(item);
              }
            });
          }).toList(),
        ));
  }

  Widget initUploadImage() {
    return GestureDetector(
        child: Container(
          child: Row(
            children: <Widget>[
              Container(
                height: 110,
                width: 110,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: NetworkImage(kImageHost + 'upload_asset.png'),
                    fit: BoxFit.fill,
                  ),
                ),
              )
            ],
          ),
//          color: Colors.black12,
        ),
        onTap: (() {
          ImageUtils.pickImageUpLoad(context,((result, b) {
            setState(() {
              widget.items.addAll(result);
            });
          }), ((error, b) {}));
        }));
  }

  void onItemClick(item) {
    //弹窗 1.查看 2.删除
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return new SimpleDialog(
          // 内容
          title: const Text('是否删除'),
          children: <Widget>[
            // 选项
            SimpleDialogOption(
              onPressed: () {
                // 路由返回，并返回结果
                setState(() {
                  widget.items.remove(item);
                });
                Navigator.of(context).pop();
              },
              child: const Text('删除'),
            ),
            SimpleDialogOption(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text('取消'),
            ),
          ],
        );
      },
    ).then((val) {});
  }
}

//独立Item
Widget _girdInkWell(BuildContext context, item, VoidCallback voidCallback) {
  return InkWell(
    onTap: () {
      voidCallback();
    },
    child: Column(
      children: <Widget>[
        ClipRRect(
          borderRadius: BorderRadius.circular(5),
          child: Image.network(
            item,
            width: 80,
            height: 80,
            fit: BoxFit.fill,
          ),
        )
      ],
    ),
  );
}
